<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.sf_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="用户Id" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户Id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:sfsq:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:sfsq:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:sfsq:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:sfsq:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sfsqList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sf_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="cardNo" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="描述" align="center" prop="ms" />
      <el-table-column label="身份证正面" align="center" prop="sfzImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.sfzImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证反面" align="center" prop="sfzfmImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.sfzfmImage" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="保证金额" align="center" prop="money" />
<!--      <el-table-column label="证明文件" align="center" prop="zmwj" />-->
      <el-table-column label="是否支付保证金" align="center" prop="isPay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_yes_no" :value="scope.row.isPay"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="审核备注" align="center" prop="checkRemark" />-->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:sfsq:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.checkStatus!='2'   &&  scope.row.checkStatus!='0'   "
            @click="checkUpdate(scope.row)"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:sfsq:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改身份审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.sf_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="描述" prop="ms">
          <el-input v-model="form.ms" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="身份证正面" prop="sfzImage">
          <image-upload v-model="form.sfzImage"/>
        </el-form-item>
        <el-form-item label="身份证反面" prop="sfzfmImage">
          <image-upload v-model="form.sfzfmImage"/>
        </el-form-item>
        <el-form-item label="游戏类型" prop="gameId">
          <el-select v-model="form.gameId" placeholder="请选择游戏类型" multiple clearable>
            <el-option
              v-for="game in gameList"
              :key="game.id"
              :label="game.name"
              :value="game.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="保证金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入保证金额" />
        </el-form-item>
        <el-form-item label="证明文件" prop="zmwj">
          <file-upload v-model="form.zmwj"/>
        </el-form-item>
        <el-form-item label="是否支付保证金" prop="isPay">
          <el-select v-model="form.isPay" placeholder="请选择是否支付保证金">
            <el-option
              v-for="dict in dict.type.business_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="checkStatus">
          <el-select v-model="form.checkStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="审核备注" prop="checkRemark">-->
<!--          <el-input v-model="form.checkRemark" placeholder="请输入审核备注" />-->
<!--        </el-form-item>-->
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <check-oper ref="checkform"></check-oper>
  </div>
</template>

<script>
import { listSfsq, getSfsq, delSfsq, addSfsq, updateSfsq } from "@/api/business/sfsq";
import CheckOper from '@/components/CheckOper'
import { listAllGame } from "@/api/business/game";

export default {
  name: "Sfsq",
  dicts: ['check_status', 'business_yes_no', 'sf_type'],
  components: { CheckOper },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 身份审核表格数据
      sfsqList: [],
      // 游戏列表
      gameList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        cardNo: null,
        phone: null,
        ms: null,
        sfzImage: null,
        sfzfmImage: null,
        type: null,
        money: null,
        zmwj: null,
        isPay: null,
        checkStatus: null,
        checkRemark: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
    this.getGameList();
  },
  methods: {

    /** 查询游戏列表 */
    getGameList() {
      listAllGame().then(response => {
        this.gameList = response.data;
      });
    },
    checkUpdate(row) {
      // 审核
      const id = row.id
      this.$refs.checkform.initCs('business/sfsq/checkXx', id, '审核')
    },

    /** 查询身份审核列表 */
    getList() {
      this.loading = true;
      listSfsq(this.queryParams).then(response => {
        this.sfsqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        cardNo: null,
        phone: null,
        ms: null,
        sfzImage: null,
        sfzfmImage: null,
        type: null,
        money: null,
        zmwj: null,
        isPay: null,
        checkStatus: null,
        checkRemark: null,
        userId: null,
        gameId: [],
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加身份审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSfsq(id).then(response => {
        this.form = response.data;
        // 处理 gameId 字符串转数组，以便多选框正确回显
        if (this.form.gameId && typeof this.form.gameId === 'string') {
          this.form.gameId = this.form.gameId.split(',');
        }
        this.open = true;
        this.title = "查看身份审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 处理 gameId 数组转字符串，以便提交到后端
          const submitForm = Object.assign({}, this.form);
          if (submitForm.gameId && Array.isArray(submitForm.gameId)) {
            submitForm.gameId = submitForm.gameId.join(',');
          }
          if (this.form.id != null) {
            updateSfsq(submitForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSfsq(submitForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除身份审核编号为"' + ids + '"的数据项？').then(function() {
        return delSfsq(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/sfsq/export', {
        ...this.queryParams
      }, `sfsq_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
